package com.marst.first.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marst.first.bean.Menu;
import com.marst.first.bean.VO.Result;
import com.marst.first.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MenuService {
    @Autowired
    private MenuMapper menuMapper;

    public Result getMenuList(int pageNum, int pageSize, String name, String url) {

        //参数校验
        pageSize = pageSize==0 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Menu> list = menuMapper.getMenuList(name, url);
        return Result.ok(new PageInfo<>(list));
    }



    public Result getAllMenuList() {

        List<Menu> list = menuMapper.getAllMenuList();
        return Result.ok(list);
    }

    public Result addMenu(Menu menu) {
        //参数校验
        if (StringUtils.isEmpty(menu.getName())){
            return new Result(1003,"名字不能为空");
        }
        int userCountByname = menuMapper.getMenuCountByName(menu.getName());
        if (userCountByname > 0){
            return new Result(1005,"名字不能重复");
        }
        menu.setCreateTime(new Date());
        menuMapper.insert(menu);
        return Result.ok();
    }

    public Result deleteMenu(int id) {
        menuMapper.deleteById(id);
        return Result.ok();
    }

    public Result deleteMenuOnLogic(int id) {
        menuMapper.deleteMenuOnLogic(id);
        return Result.ok();
    }

    public Result getMenuById(int id) {
        Menu menu = menuMapper.selectById(id);
        return Result.ok(menu);
    }

    public Result updateMenu(Menu menu) {
        menuMapper.updateById(menu);
        return Result.ok();
    }

}
